package com.lizk.study.netty.day009;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 字符编码的研究
 *
 * @author lizhikui
 * @date 2019/7/7 9:40
 */
public class NioTest13_2 {
    public static void main(String[] args) throws IOException {
        String str = "我是好";

        //得到原始的字节数组
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);


        //字节数组转换为byteBuffer
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);


        Charset iso = Charset.forName("iso-8859-1");

        //使用iso编码对原始字节进行编码和解码，得到新的byteBuffer
        ByteBuffer newByteBuffer = iso.encode(iso.decode(byteBuffer));


        //按照utf-8编码输出字符串
        System.out.println(new String(newByteBuffer.array(), StandardCharsets.UTF_8));


    }
}
